/**
 * Created by tanshuqiang on 16/9/22.
 */

var fs = require('fs');
var path = require('path');
var child_process = require('child_process');
var spawn = child_process.spawn;
var execSync = child_process.execSync;
var simpleGit = require('simple-git');

var workspacesPath = "/paasdb/workspaces";
var packageFilePath = path.join(__dirname, 'data.json');
var gitServer = "123.56.190.188";

try{
    fs.accessSync(packageFilePath, fs.R_OK);
}catch(err){
    console.log('cann not access file :'+packageFilePath);
    return;
}

var packageJSON = require(packageFilePath);

start(packageJSON);

function start(packageJSON){
    for(var key in packageJSON){
        var workspace = {};
        workspace.name = key;
        workspace.project = packageJSON[key].project;
        workspace.namespace = packageJSON[key].user;
        refactorWorkspace(workspace);
    }
    console.log("done");
}

function refactorWorkspace(workspace) {
    try {
        var oldWorkspacePath = path.join(workspacesPath, 'workspace'+workspace.name);
        var exist = fs.existsSync(oldWorkspacePath);
        if(!exist){
            console.log('can not find old workspace:' + workspace.name);
            return;
        }

        var newWorkspacePath = path.join(workspacesPath, workspace.namespace, workspace.project, workspace.name);
        var metadataPath = path.join(newWorkspacePath, '.metadata');

        exist = fs.existsSync(path.join(workspacesPath, workspace.namespace));
        if(!exist){
            fs.mkdirSync(path.join(workspacesPath, workspace.namespace));
        }

        exist = fs.existsSync(path.join(workspacesPath, workspace.namespace, workspace.project));
        if(!exist){
            fs.mkdirSync(path.join(workspacesPath, workspace.namespace, workspace.project));
        }

        exist = fs.existsSync(newWorkspacePath);
        if(exist){
            execSync("rm -rf " + newWorkspacePath);
        }

        execSync("mv -f " + oldWorkspacePath + " " + newWorkspacePath);
        console.log('copy workspace ' + workspace.name + ' to new location success!');

        execSync("rm -rf " + metadataPath);
        console.log('rm workspace ' + workspace.name + '  metadata success!');

        var repoPath = path.join(newWorkspacePath, 'source', workspace.project);
        var newRemotePath = "git@"+gitServer+":"+workspace.namespace+"/"+workspace.project;

        modifyRemoteRepo(repoPath, newRemotePath, 'develop', workspace.name);
        console.log('refactor workspace ' + workspace.name + ' success!');
    }catch (e){
        console.log('refactor workspace ' + workspace.name + ' failed!');
    }
}

function modifyRemoteRepo(repoPath, newRemotePath, branchName, workspaceName){
    //console.log('modify ['+workspaceName+']['+repoPath+'] remote branch to ['+newRemotePath+'] about ['+branchName+']');
    var git = simpleGit(repoPath);

    git.removeRemote('origin')
        .addRemote('origin', newRemotePath)
        .push(['-u', 'origin', branchName], function (err) {
            if(!err){
                console.log('modify workspace '+ workspaceName + ' remote repo success!');
            }else{
                console.log('modify workspace '+ workspaceName + ' remote repo failed!');
            }
        });
}




